package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.constant.SystemPermissions;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dao.mapper.PermissionMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteEmployeeRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import com.cskaoyan.wordmemorize.request.EmployeePageRequest;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class EmployeeController {


    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    EmployeeService employeeService;


    /**
     * 获取当前用户登陆信息
     * @return
     */
    @GetMapping("/admin/employee/info")
//    @SaAdminCheckPermission(SystemPermissions.Employee.SEARCH)
    public Result<EmployeeDTO> getUserInfo() {
        long loginIdAsLong = StpKit.ADMIN.getLoginIdAsLong();
//        long loginIdAsLong = 1858808795138543618l;
        EmployeeDTO employeeInfo = employeeService.getEmployeeInfo(loginIdAsLong);
        return Result.ok(employeeInfo);
    }

    /**
     * 更新或者上传图片
     * @param file
     * @return
     */
    @PostMapping("/admin/employee/avatar")
    @SaAdminCheckPermission(SystemPermissions.Employee.UPDATE)
    public Result<String> postAvatar(@RequestBody MultipartFile file){
        EmployeeDTO employeeDTO = employeeService.uploadAvatar(file);
        return Result.ok(employeeDTO.getAvatar());
    }

    /**
     *  添加员工
     * @param employeeCommand
     * @return
     */
    @PostMapping("/admin/employee")
    @SaAdminCheckPermission(SystemPermissions.Employee.ADD)
    public Result addEmployee(@RequestBody EmployeeCommand employeeCommand){
        employeeService.addEmployee(employeeCommand);
        return Result.ok();
    }

    /**
     * 更新员工数据
     * @return
     */
    @PutMapping("/admin/employee")
    @SaAdminCheckPermission(SystemPermissions.Employee.UPDATE)
    public Result updateEmployee(@RequestBody EmployeeCommand employeeCommand){
        employeeService.updateEmployee(employeeCommand);
        return Result.ok();
    }

    /**
     * 后台获取员工列表
     * @param pageRequest
     * @return
     */
    @GetMapping("/admin/employee/list")
    @SaAdminCheckPermission(SystemPermissions.Employee.SEARCH)
    public Result<PageDTO<EmployeeDTO>> getAllEmployeeList(EmployeePageRequest pageRequest ){
        PageDTO<EmployeeDTO> employeePage = employeeService.getEmployeePage(pageRequest);
        return Result.ok(employeePage);
    }

    /**
     * 后台删除员工信息
     * @param employeeId
     * @return
     */
    @DeleteMapping("/admin/employee/{employeeId}")
    @SaAdminCheckPermission(SystemPermissions.Employee.DELETE)
    public Result deleteEmployee(@PathVariable("employeeId") Long employeeId){
        employeeService.delete(employeeId);
        return Result.ok();
    }


    /**
     * 删除员工角色关系
     * @param employeeId
     * @param roleId
     * @return
     */
    @DeleteMapping("/admin/employee/{employeeId}/role/{roleId}")
    @SaAdminCheckPermission(SystemPermissions.Employee.DELETE)
    public Result deleteEmployeeToRole(@PathVariable ("employeeId")String employeeId ,
                                       @PathVariable ("roleId") String roleId){
        DeleteEmployeeRoleOperateDTO deleteEmployeeRoleOperateDTO = employeeService.deleteRole(Long.valueOf(employeeId), Long.valueOf(roleId));

        return Result.ok(deleteEmployeeRoleOperateDTO);
    }


}
